package com.shycloud.mido.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.member.api.entity.MultiClassroom;
import com.shycloud.mido.member.api.entity.MultiClassroomMemberAttend;
import com.shycloud.mido.member.api.entity.MultiStudentClassroomLink;
import com.shycloud.mido.member.api.entity.MultiTeacherClassroomLink;
import com.shycloud.mido.member.api.entity.OrgTeacherDetailed;
import com.shycloud.mido.member.api.vo.AppMultiStudentClassroomVO;
import com.shycloud.mido.member.api.vo.MyTeacherVO;
import com.shycloud.mido.member.mapper.MultiClassroomMemberAttendMapper;
import com.shycloud.mido.member.mapper.MultiTeacherClassroomLinkMapper;
import com.shycloud.mido.member.mapper.OrgTeacherDetailedMapper;
import com.shycloud.mido.member.service.MultiClassroomService;
import com.shycloud.mido.member.service.MultiStudentClassroomLinkService;
import com.shycloud.mido.member.service.MultiTeacherClassroomLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.member.service.TeacherService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@Service
@AllArgsConstructor
public class MultiTeacherClassroomLinkServiceImpl extends
		ServiceImpl<MultiTeacherClassroomLinkMapper, MultiTeacherClassroomLink> implements MultiTeacherClassroomLinkService {

	/** 教师模块 mapper */
	private final OrgTeacherDetailedMapper teacherDetailedMapper;
	/** 加入班级记录信息 mapper */
	private final MultiClassroomMemberAttendMapper attendMapper;
	/** 学生班级模块 service */
	private final MultiStudentClassroomLinkService multiStudentClassroomLinkService;
	/** 班级模块 service */
	private final MultiClassroomService multiClassroomService;
	/** 个人教师模块 service */
	private final TeacherService teacherService;

	@Override
	public Boolean addTeacher(MultiTeacherClassroomLink teacherClassroomLink) {

		//判断教师是否在此教室中
		Integer count = baseMapper.selectCount(new QueryWrapper<MultiTeacherClassroomLink>()
				.eq("teacher_id", teacherClassroomLink.getTeacherId())
				.eq("classroom_id", teacherClassroomLink.getClassroomId()));

		if (count > 0) {
			throw new BusinessException("此教师已在班级中");
		}

		//获取教师详情
		OrgTeacherDetailed orgTeacherDetailed = teacherDetailedMapper.selectById(teacherClassroomLink.getTeacherId());

		if (orgTeacherDetailed == null) {
			throw new BusinessException("教师查询失败,请确认 teacherId 是否正确");
		}

		MultiClassroomMemberAttend attend = addMultiClassroomMemberAttend(orgTeacherDetailed, "0");

		//2.添加教师进教室
		//记录表ID
		teacherClassroomLink.setAttendId(attend.getId());
		//创建时间
		teacherClassroomLink.setCreateTime(LocalDateTime.now());
		//有效数据
		teacherClassroomLink.setDelFlag(CommonConstants.STATUS_NORMAL);
		baseMapper.insert(teacherClassroomLink);

		return true;

	}

	private MultiClassroomMemberAttend addMultiClassroomMemberAttend(OrgTeacherDetailed orgTeacherDetailed, String attendType) {

		//1.添加记录信息
		MultiClassroomMemberAttend attend = new MultiClassroomMemberAttend();
		//用户 MemberId
		attend.setAppointUserId(orgTeacherDetailed.getMemberId());
		//申请时间
		attend.setAttendTime(LocalDateTime.now());
		//申请类型 0加入 1退出
		attend.setAttendType(attendType);
		//申请客户端 - web
		attend.setAttendUserClient("0");
		//默认机构审核通过
		attend.setCheckerType("2");
		//有效数据
		attend.setDelFlag(CommonConstants.STATUS_NORMAL);
		//已通过
		attend.setStatus("1");
		//教师
		attend.setType("1");
		//创建时间
		attend.setCreateTime(LocalDateTime.now());
		attendMapper.insert(attend);
		return attend;
	}

	@Override
	public List<MyTeacherVO> getTeacherListByRoomId(String classroomId) {
		return baseMapper.getTeacherListByRoomId(classroomId, null);
	}

	@Override
	public boolean delTeacher(String teacherId, String classroomId) {

		//获取学生信息
		OrgTeacherDetailed orgTeacherDetailed = teacherDetailedMapper.selectById(teacherId);
		//1.踢出学生
		baseMapper.delete(new QueryWrapper<MultiTeacherClassroomLink>()
				.eq("teacher_id", teacherId)
				.eq("classroom_id", classroomId));
		//2.记录信息
		addMultiClassroomMemberAttend(orgTeacherDetailed, "1");

		return true;
	}

	@Override
	public Map<String, List<AppMultiStudentClassroomVO>> getTeacherList(String studentId, String teacherName) {

		List<AppMultiStudentClassroomVO> classList = new ArrayList<>();

		//根据学生ID获取班级列表
		List<Object> classroomIds = multiStudentClassroomLinkService
				.listObjs(new QueryWrapper<MultiStudentClassroomLink>()
						.select("classroom_id")
						.eq("student_id", studentId));

		//循环班级列表,查询教师数据
		for (Object classroomId : classroomIds) {
			//获取班级信息
			MultiClassroom multiClassroom = multiClassroomService.getById((String) classroomId);
			AppMultiStudentClassroomVO appMultiStudentClassroomVO = new AppMultiStudentClassroomVO();
			appMultiStudentClassroomVO.setClassOrGroupName(multiClassroom.getName());
			appMultiStudentClassroomVO.setClassroomId(multiClassroom.getId());
			appMultiStudentClassroomVO.setScope(multiClassroom.getScope());
			//获取班级教师信息
			List<MyTeacherVO> teacherList = baseMapper.getTeacherListByRoomId((String) classroomId, teacherName);
			if (teacherList != null) {
				appMultiStudentClassroomVO.setTeacherS(teacherList);
			}
			appMultiStudentClassroomVO.setTeacherCount(teacherList.size());
			classList.add(appMultiStudentClassroomVO);

		}

		//==================== 构建个人教师和[斑马目标助手] ============================================//

		//定义个人教师
		List<AppMultiStudentClassroomVO> groupList = new ArrayList<>();

		AppMultiStudentClassroomVO appMultiStudentClassroomVO = new AppMultiStudentClassroomVO();
		//构建教师信息
		List<MyTeacherVO> privateTeachers = new ArrayList<>(2);
		//获取个人教师信息
		MyTeacherVO myTeacher = teacherService.getMyTeacher(studentId);

		//全部显示的场合
		if (StringUtils.isBlank(teacherName)) {
			if (myTeacher != null) {
				privateTeachers.add(myTeacher);
			}
		}
		//搜索的场合
		else {
			if (myTeacher != null && StringUtils.contains(myTeacher.getName(), teacherName)) {
				privateTeachers.add(myTeacher);
			}
		}
		appMultiStudentClassroomVO.setTeacherS(privateTeachers);
		groupList.add(appMultiStudentClassroomVO);

		//定义目标作业教师(自己)
		List<AppMultiStudentClassroomVO> targetList = new ArrayList<>();
		AppMultiStudentClassroomVO appMultiStudentClassroomVOT = new AppMultiStudentClassroomVO();
		List<MyTeacherVO> privateTeachersT = new ArrayList<>();
		if (StringUtils.isBlank(teacherName)) {
			//构建[斑马目标助手]
			MyTeacherVO zebraTarget = new MyTeacherVO();
			zebraTarget.setName("斑马目标助手");
			zebraTarget.setTeacherId("parentMode");
			zebraTarget.setMemberShowName("斑马目标助手");
			privateTeachersT.add(zebraTarget);
			appMultiStudentClassroomVOT.setTeacherS(privateTeachersT);
		} else {
			if (StringUtils.contains("斑马目标助手", teacherName)) {
				MyTeacherVO zebraTarget = new MyTeacherVO();
				zebraTarget.setName("斑马目标助手");
				zebraTarget.setTeacherId("parentMode");
				zebraTarget.setMemberShowName("斑马目标助手");
				privateTeachersT.add(zebraTarget);
				appMultiStudentClassroomVOT.setTeacherS(privateTeachersT);
			}
		}
		targetList.add(appMultiStudentClassroomVOT);
		Map<String, List<AppMultiStudentClassroomVO>> resMap = new HashMap(3);
		resMap.put("groupList", groupList);
		resMap.put("classList", classList);
		resMap.put("targetList", targetList);
		return resMap;
	}

	@Override
	public MultiClassroom getClassRoomIdByMemberId(String memberId) {
		return baseMapper.getClassRoomByMemberId(memberId);
	}

	@Override
	public boolean addTeacherApp(String teaUsername, MultiClassroom classroom) {

		//判断此用户是否是教师身份
		OrgTeacherDetailed orgTeacherDetailed = teacherService.getTeacherByUsername(teaUsername);

		if (orgTeacherDetailed == null) {
			throw new BusinessException("此用户不是教师身份");
		}

		//判断教师是否在此教室中
		Integer count = baseMapper.selectCount(new QueryWrapper<MultiTeacherClassroomLink>()
				.eq("teacher_id", orgTeacherDetailed.getId())
				.eq("classroom_id", classroom.getId()));

		if (count > 0) {
			throw new BusinessException("此教师已在班级中");
		}

		MultiClassroomMemberAttend attend = addMultiClassroomMemberAttend(orgTeacherDetailed, "0");

		//2.添加教师进教室
		MultiTeacherClassroomLink teacherClassroomLink = new MultiTeacherClassroomLink();
		//班级ID
		teacherClassroomLink.setClassroomId(classroom.getId());
		//教师ID
		teacherClassroomLink.setTeacherId(orgTeacherDetailed.getId());
		//记录表ID
		teacherClassroomLink.setAttendId(attend.getId());
		//创建时间
		teacherClassroomLink.setCreateTime(LocalDateTime.now());
		//有效数据
		teacherClassroomLink.setDelFlag(CommonConstants.STATUS_NORMAL);

		baseMapper.insert(teacherClassroomLink);
		return true;
	}

	@Override
	public List<MyTeacherVO> getTeacherList(String studentId) {

		List<MyTeacherVO> teacherListAll = new ArrayList<>();

		//个人教师
		MyTeacherVO myTeacher = teacherService.getMyTeacher(studentId);
		teacherListAll.add(myTeacher);

		return teacherListAll;

//		//班级教师
//		//根据学生ID获取班级列表
//		List<Object> classroomIds = multiStudentClassroomLinkService
//				.listObjs(new QueryWrapper<MultiStudentClassroomLink>()
//						.select("classroom_id")
//						.eq("student_id", studentId));
//
//		//循环班级列表,查询教师数据
//		for (Object classroomId : classroomIds) {
//			//获取班级信息
//			MultiClassroom multiClassroom = multiClassroomService.getById((String) classroomId);
//			AppMultiStudentClassroomVO appMultiStudentClassroomVO = new AppMultiStudentClassroomVO();
//			appMultiStudentClassroomVO.setClassOrGroupName(multiClassroom.getName());
//			appMultiStudentClassroomVO.setClassroomId(multiClassroom.getId());
//			appMultiStudentClassroomVO.setScope(multiClassroom.getScope());
//			//获取班级教师信息
//			List<MyTeacherVO> teacherList = baseMapper.getTeacherListByRoomId((String) classroomId, null);
//			if (myTeacher != null && teacherList != null) {
//				//若班级教师正好也是此学生的个人教师
//				if (teacherList.stream().anyMatch(x -> myTeacher.getTeacherId().equals(x.getTeacherId()))) {
//					teacherListAll = new ArrayList<>();
//				}
//			}
//			teacherListAll.addAll(teacherList);
//		}
//
////		if (CollectionUtils.isEmpty(teacherListAll)) {
////			return teacherListAll;
////		}
//
//		return teacherListAll.stream().distinct().collect(Collectors.toList());

	}
}
